package com.eDriveV2;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.Window;
import android.webkit.WebView;


public class ChartView extends Activity {
	private ArrayList<Integer> speedData = new ArrayList();
	private ArrayList<Integer> effData = new ArrayList();
	private String speedString = "";
	private String effString = "";
	private String url;
	private String fileName;
	
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    requestWindowFeature(Window.FEATURE_NO_TITLE);
	    setContentView(R.layout.chartview);
	    
	    Bundle extras = getIntent().getExtras();
	    fileName = extras.getString("fileName");
	    
	    XMLReader xml = new XMLReader(fileName);
	    ArrayList[] array = xml.readXML();
	    speedData = array[2];
	    effData = array[3];
	    fillSpeedString(speedData);
	    fillEffString(effData);
	    
	    Display display = getWindowManager().getDefaultDisplay(); 
	    int width = display.getWidth() - 10;
	    int height = display.getHeight() - 50;
	    
	    WebView mWebView;
	    mWebView = (WebView) findViewById(R.id.webview);
	    mWebView.getSettings().setJavaScriptEnabled(true);
	    url = "http://chart.apis.google.com/chart?"+
              "chxr=0,-5,100"+
              "&chxt=x,y&"+
              "chs="+width+"x"+height+
              "&cht=lxy"+
              "&chco=FF0000,FF9900"+
              "&chds=0,100,0,125,0,100,0,125"+
              "&chd=t:-1|"+speedString+"|-1|"+effString+
              "&chdl=Speed|Efficiency"+
              "&chdlp=b"+
              "&chls=1"+
              "&chma=5,5,5,25|0,5"+
              "&chtt=eDrive+data";
	    mWebView.loadUrl(url);
	    Log.d("speedString", speedString);
	    Log.d("url", url);
	}
	
	private void fillSpeedString(ArrayList<Integer> speedData) {
		for (int i = 0; i < speedData.size(); i++) {
			if (i != speedData.size() - 1) {
				speedString += speedData.get(i) + ",";
			} else {
				speedString += speedData.get(i);
			}
			Log.d("speedData", String.valueOf(speedData.get(i)));
		}
	}

	private void fillEffString(ArrayList<Integer> effData) {
		for (int i = 0; i < effData.size(); i++) {
			if (i != effData.size() - 1) {
				effString += effData.get(i) + ",";
			} else {
				effString += effData.get(i);
			}
		}
	}
}